package repo

import (
	"time"

	"github.com/jinzhu/gorm"

	"github.com/pingcap-incubator/tidb-dashboard/pkg/model"
)

// GetInstancesByUserID 根据用户权限查询所能访问的实例
func GetInstancesByUserID(db *gorm.DB, userID string) ([]model.TidbInstance, error) {
	var instances []model.TidbInstance
	err := db.Table(InstanceTable).
		Select(" tidb_instance.id, name ").
		Joins("left join tidb_dashboard.tidb_instance_permission as p on tidb_instance.id = p.instance_id  ").
		Where("tidb_instance.status=1 and p.user_id = ? ", userID).
		Order("tidb_instance.index ASC").
		Scan(&instances).
		Error
	if err != nil {
		return nil, err
	}
	return instances, nil
}

// GetInstanceByUserID 根据用户权限和实例ID查询该实例
func GetInstanceByUserID(db *gorm.DB, userID string, instanceID uint) (*model.TidbInstance, error) {
	var instances []model.TidbInstance
	err := db.Table(InstanceTable).
		Model(" tidb_instance.* ").
		Joins("left join tidb_dashboard.tidb_instance_permission as p on tidb_instance.id = p.instance_id  ").
		Where("tidb_instance.status=1 and p.user_id = ? and p.instance_id = ? ", userID, instanceID).
		Find(&instances).
		Error
	if err != nil {
		return nil, err
	}
	if len(instances) == 0 {
		return nil, nil
	}
	return &instances[0], nil
}

// AddInstancePermission 添加实例访问权限
func AddInstancePermission(db *gorm.DB, instanceID uint, userID string) error {
	t := time.Now()
	if err :=
		db.Table(PermissionTable).
			Set("gorm:insert_modifier", "IGNORE").
			Create(&model.TidbInstancePermission{InstanceID: instanceID, UserID: userID, CreateTime: &t}).
			Error; err != nil {
		return ErrExecute.Wrap(err, "insert user role error")
	}
	return nil
}

// DeleteInstancePermission 删除实例访问权限
func DeleteInstancePermission(db *gorm.DB, userID string, instanceIDs []uint) error {
	if err :=
		db.Table(PermissionTable).
			Where("user_id = ?", userID).
			Where("instance_id in (?)", instanceIDs).
			Delete(model.TidbInstancePermission{}).Error; err != nil {
		return ErrExecute.Wrap(err, "delete user instance permission error")
	}
	return nil
}

// DeleteInstancePermissionByUserID 根据用户ID删除用户实例访问权限
func DeleteInstancePermissionByUserID(db *gorm.DB, userID string) error {
	if err :=
		db.Table(PermissionTable).
			Where("user_id = ?", userID).
			Delete(model.TidbInstancePermission{}).Error; err != nil {
		return ErrExecute.Wrap(err, "delete user instance error")
	}
	return nil
}

// DeleteInstancePermissionByInstanceID 根据实例ID删除用户实例访问权限
func DeleteInstancePermissionByInstanceID(db *gorm.DB, instanceID uint) error {
	if err :=
		db.Table(PermissionTable).
			Where("instance_id = ?", instanceID).
			Delete(model.TidbInstancePermission{}).Error; err != nil {
		return ErrExecute.Wrap(err, "delete user instance error")
	}
	return nil
}

// DeleteInstancePermissionByID 根据ID删除用户实例访问权限
func DeleteInstancePermissionByID(db *gorm.DB, ID uint) error {
	if err :=
		db.Table(PermissionTable).
			Where("id = ?", ID).
			Delete(model.TidbInstancePermission{}).Error; err != nil {
		return ErrExecute.Wrap(err, "delete user instance error")
	}
	return nil
}

// GetUserPermissionByInstanceID 根据实例ID查询该实例的访问权限
func GetUserPermissionByInstanceID(db *gorm.DB, instanceID uint) ([]model.GetUserPermission, error) {
	var userPermissions []model.GetUserPermission
	err := db.Table(PermissionTable).
		Select(" tidb_instance_permission.id, tidb_instance_permission.create_time, u.user_id, u.description, u.name ").
		Joins("left join tidb_dashboard.user as u on tidb_instance_permission.user_id = u.user_id  ").
		Where("tidb_instance_permission.instance_id = ? ", instanceID).
		Find(&userPermissions).
		Error
	if err != nil {
		return nil, err
	}
	return userPermissions, nil
}
